<?php

namespace app\models;

use app\models\user\User;
use Yii;

/**
 * This is the model class for table "queue_scan_pay".
 *
 * @property int $id
 * @property string $create_time
 * @property string $update_time
 * @property int $order_id
 * @property int $user_id
 * @property int $status 0 排队中, 1 已完成 2 已中奖
 * @property int $merchant_id
 * @property float $amount
 * @property float $real_amount
 */
class QueueScanPay extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'queue_scan_pay';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['create_time', 'update_time'], 'safe'],
            [['order_id', 'user_id', 'status', 'merchant_id'], 'integer'],
            [['amount', 'real_amount'], 'number'],
            [['real_amount'], 'required'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'order_id' => 'Order ID',
            'user_id' => 'User ID',
            'status' => 'Status',
            'merchant_id' => 'Merchant ID',
            'amount' => 'Amount',
            'real_amount' => 'Real Amount',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id'])
            ->select(['id', 'mobile', 'avatar', 'nickname']);
    }
}
